package com.javaee.shortlink.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.javaee.shortlink.admin.common.convention.result.Result;
import com.javaee.shortlink.admin.common.convention.result.Results;
import com.javaee.shortlink.admin.common.enums.UserErrorCodeEnum;
import com.javaee.shortlink.admin.dto.req.UserLoginReqDTO;
import com.javaee.shortlink.admin.dto.req.UserRegisterReqDTO;
import com.javaee.shortlink.admin.dto.req.UserUpdateReqDTO;
import com.javaee.shortlink.admin.dto.resp.ActualUserRespDTO;
import com.javaee.shortlink.admin.dto.resp.UserLoginRespDTO;
import com.javaee.shortlink.admin.dto.resp.UserRespDTO;
import com.javaee.shortlink.admin.service.GroupService;
import com.javaee.shortlink.admin.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理控制层
 */
@RestController
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;

    /**
     *根据用户名返回用户信息
     */
    @GetMapping("/api/short-link/admin/v1/user/{username}")
    public Result<UserRespDTO> getUserByUsername(@PathVariable("username") String username) {
        return Results.success(userService.getUserByUsername(username));
    }

    /**
     *根据用户名返回无脱敏用户信息
     */
    @GetMapping("/api/short-link/admin/v1/actual/user/{username}")
    public Result<ActualUserRespDTO> getActualUserByUsername(@PathVariable("username") String username) {
        return Results.success(BeanUtil.toBean(userService.getUserByUsername(username),ActualUserRespDTO.class));
    }

    /**
     *查询用户名是否已存在
     */
    @GetMapping("/api/short-link/admin/v1/user/has-username")
    public Result<Boolean> hasUserName(@RequestParam String username) {
        return Results.success(userService.hasUsername(username));
    }

    /**
     *注册用户
     */
    @PostMapping("/api/short-link/admin/v1/user")
    public Result<Void>register(@RequestBody UserRegisterReqDTO userRegisterReqDTO) {
        userService.register(userRegisterReqDTO);
        return Results.success();
    }

    /**
     *更新用户信息
     */
    @PutMapping("/api/short-link/admin/v1/user")
    public Result<Void>update(@RequestBody UserUpdateReqDTO userUpdateReqDTO){
        userService.update(userUpdateReqDTO);
        return Results.success();
    }

    /**
     *用户登录
     */
    @PostMapping("/api/short-link/admin/v1/user/login")
    public Result<UserLoginRespDTO>login(@RequestBody UserLoginReqDTO userLoginReqDTO){
        return Results.success(userService.login(userLoginReqDTO));
    }

    /**
     * 检查用户是否登录
     */
    @GetMapping("/api/short-link/admin/v1/user/check-login")
    public Result<Boolean> checkLogin(@RequestParam("username") String username, @RequestParam("token") String token){
        return Results.success(userService.checkLogin(username,token));
    }

    /**
     * 用户注销
     */
    @DeleteMapping("/api/short-link/admin/v1/user/logout")
    public Result<Void>logout(@RequestParam("username") String username, @RequestParam("token") String token){
        userService.logout(username,token);
        return Results.success();
    }
}
